import { PlayGround_Listen, UI_Layer } from "../../config/ui_def"
import { getLongScreen } from "../tool/util";

cc.Class({
    extends: cc.Component,

    properties: {
        role_content_node : cc.Node,        // 角色 Buff 节点
        common_content_node : cc.Node,      // 通用 Buff 节点
        
        hp_label: cc.Label,                 // 生命值label
        hp_sprite: cc.Sprite,               // 生命值进度
        avatar_sprite: cc.Sprite,           // 角色头像信息

        hp_ani:0,                           // 生命值动画值
        hp_ani_switch:false,                //

        widget: cc.Widget,
    },
    initPage(){
        if(getLongScreen()){
            this.widget.top = 50
        }else{
            this.widget.top = 0
        }
        this.widget.updateAlignment()
    },
    updatePage(){
        this.hp_ani_max = 0
        gg.eventManager.on('topInfo',this.listenClient,this)
    },
    listenClient(code,param,arg){
        if(code){
            switch(code){
                case PlayGround_Listen.UPDATE_HP:
                    this.updateHp(param)
                break
                case PlayGround_Listen.UPDATE_HP_ANI:
                    this.hp_ani_switch = true
                break
                case PlayGround_Listen.UPDATE_AVATAR:
                    this.avatar_sprite.spriteFrame = he.load.get(`fightMain`, cc.SpriteAtlas).getSpriteFrame('head_' +  param)
                    break
            }
        }
    },
    // 血条检测
    aniHp(){
        if(gg.data.hp != this.hp_ani){
            if(this.hp_ani > gg.data.hp){
                this.hp_ani_max -= 1
                let hp_res = this.hp_ani_max + this.hp_ani
                if(hp_res < gg.data.hp){
                    hp_res = gg.data.hp
                    this.hp_ani_max = 0
                }
                this.hp_ani = hp_res
            }else if(this.hp_ani < gg.data.hp){
                this.hp_ani_max += 1
                let hp_res = this.hp_ani_max + this.hp_ani
                if(hp_res > gg.data.hp){
                    hp_res = gg.data.hp
                    this.hp_ani_max = 0
                }
                this.hp_ani = hp_res
            }else{
                this.hp_ani_max = 0
                this.hp_ani_switch = false
            }

            if(this.hp_ani < 0){
                this.hp_ani = 0
                this.hp_ani_max = 0
            }
            this.updateHp(this.hp_ani)
        }
    },
    // 直接更新血条
    updateHp(hpNum){
        if(hpNum){
            this.hp_label.string = `${hpNum}`
            this.hp_sprite.fillRange = hpNum / gg.data.hpMax
        }else{
            this.hp_ani = gg.data.hp
            this.hp_label.string = gg.data.hp
            this.hp_sprite.fillRange = 1
        }
    },
    // 打开暂停
    openPause(){
        gg.uiManager.addPage(UI_Layer.Pop,{
            name: 'pausePage',
            cover: true,
            close: true
        })
    },
    // 关闭
    closeUI(){
        gg.eventManager.off("topInfo", this.listenClient, this)
    },
    // 此处要改现在 临时卸载Update
    update (dt) {
        if(this.hp_ani_switch){
            this.aniHp()
        }
    },
});
